/*
 * WebConfig.java
 * Copyright 2021 Qunhe Tech, all rights reserved.
 * Qunhe PROPRIETARY/CONFIDENTIAL, any form of usage is subject to approval.
 */

package top.bbman.boot.auth.config;/*
 * WebConfig.java
 * Copyright 2018 Qunhe Tech, all rights reserved.
 * Qunhe PROPRIETARY/CONFIDENTIAL, any form of usage is subject to approval.
 */

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.bbman.boot.auth.interceptor.*;
import top.bbman.boot.repository.constant.PeaConfigName;

import java.util.Arrays;
import java.util.List;

@Configuration(PeaConfigName.Auth.WEB)
@RefreshScope
public class WebConfig implements WebMvcConfigurer {

    @Value("${bb-man.auth.web-config.exclude-path:}")
    private List<String> excludePath;

    @Value("${bb-man.auth.web-config.exclude-path-auth:}")
    private List<String> excludePathAuth;

    private static final List<String> EXCLUDE_PATH_ = Arrays.asList("/js/**", "/css/**", "/images/**");

    @Bean
    public PreLoginInterceptor getPreLoginInterceptor() {
        return new PreLoginInterceptor();
    }

    @Bean
    public CROSInterceptor getCROSInterceptor() {
        return new CROSInterceptor();
    }


    @Bean
    public WapLoginInterceptor getWapLoginInterceptor() {
        return new WapLoginInterceptor();
    }

    @Bean
    public WapAjaxAuthInterceptor getWapAjaxAuthInterceptor() {
        return new WapAjaxAuthInterceptor();
    }

    @Bean
    public ThreadLocalInterceptor getThreadLocalInterceptor() {
        return new ThreadLocalInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getPreLoginInterceptor()).addPathPatterns("/**")
                .excludePathPatterns(this.excludePath);
        registry.addInterceptor(getCROSInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(getWapLoginInterceptor()).addPathPatterns("/**")
                .excludePathPatterns(this.excludePath);
        registry.addInterceptor(getWapAjaxAuthInterceptor()).addPathPatterns("/**")
                .excludePathPatterns(this.excludePathAuth);
        registry.addInterceptor(getThreadLocalInterceptor()).addPathPatterns("/**")
                .excludePathPatterns(EXCLUDE_PATH_);
    }
}
